#!/bin/sh
extra_paths="/opt/local/bin:/usr/pkg/bin"
for dir in `echo echo $extra_paths $PATH | sed s/:/" "/g | xargs -n1 echo \
             | sort -u`
do
  if [ -x "$dir/python2.5" ]; then
    python="$dir/python2.5"
  elif [ -x "$dir/python2.4" ]; then
    python="$dir/python2.4"
  elif [ -x "$dir/python" ]; then
    python="$dir/python"
  fi
done

if [ "$python" = "" ]; then
  echo "Sorry, we could not find a python interpreter installed."
  exit 2
fi

os=`uname`
os_type=`$python sysinfo.py Platform`
version=`grep VERSION version.py | cut -d\' -f2 | cut -d- -f1`
dist="${os_type}-binaries-$version"
pkg_pointer=".${dist}"
compile_subdir=""

trace_file="test/${dist}.trace"
manifest_file="test/${dist}.manifest"
archive_file="${dist}.tar.gz"

trace_cmd="strace -o $trace_file -f"
root_dir=`pwd`
compiled_dir="$root_dir/compiled/"
package_dir="$root_dir/packaging"
echo "Creating package for $dist ($archive_file)"

if [ ! -x "./freebench" ]; then
  echo "This must be run from the freebench directory"
fi

if [ ! -f "$trace_file" ]; then
  case $os in
    Darwin)
      dtruss="dtruss -f ./freebench -n1"
      if [ `id -u` != 0 ]; then
        echo "Trying to run: $dtruss 2>$trace_file"
        echo "dtruss requires root privileges. If prompted, type your password in below."
        sudo $dtruss 2>$trace_file
      else
        $dtruss 2>$trace_file
      fi
    ;;
    Linux)
      strace -o $trace_file -f ./freebench -n1
    ;;
    SunOS)
      truss -o $trace_file -f ./freebench -n1
    ;;
    FreeBSD|OpenBSD|NetBSD)
      ktrace -di -f ${trace_file}.raw ./freebench -n1
      echo "Translating ktrace -> kdump"
      kdump -f ${trace_file}.raw > ${trace_file}
    ;;
  esac
  echo "Trace complete."
fi


if [ ! -s "$trace_file" ]; then
  echo "Could not create trace file"
  exit 2
else
  echo "Found trace file: $trace_file"
fi

echo "Processing Trace"
grep "$compiled_dir" $trace_file | cut -d\" -f2 | cut -d"\\" -f1 | sort -u | grep "^$compiled_dir" > $manifest_file

if [ ! -s "$manifest_file" ]; then
  echo "Could not create manifest $manifest_file"
  exit 3
else
  echo "Found manifest: $manifest_file"
fi

echo "Clearing $package_dir/"
rm -Rf "$package_dir/"
mkdir -p "$package_dir"

for file in `grep "$compiled_dir" $manifest_file`
do
  if [ "$compile_subdir" = "" ]; then
    relative_path=`echo $file | perl -pe "s#$compiled_dir##" | grep "/"`
    if [ "$relative_path" != "" ]; then
      compile_subdir=`echo $relative_path | grep "/" | cut -d/ -f1`
      echo "Compile subdir is '$compile_subdir' (from $relative_path)"
    fi
  fi
  new_file=`echo $file | perl -pe "s#$compiled_dir#$package_dir\/#"`
  if [ -d "$file" ]; then
    mkdir -p $new_file
  elif [ -f "$file" ]; then
    dir=`dirname $new_file`
    mkdir -p $dir
    cp -p $file $new_file
    if [ "`echo $new_file | grep bin/`" != "" ]; then
      echo "stripping $new_file"
      strip $new_file
    fi
  fi
done

$python sysinfo.py > packaging/$compile_subdir/.sysinfo
cd packaging
echo $compile_subdir > $pkg_pointer
echo "creating $archive_file"
tar -cf - * $pkg_pointer | gzip -c > $archive_file
cd ..
ls -lad packaging/$archive_file
